package bupt.party.online.video;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

public class Video implements Comparable<Video> {
	private String picPath;
	private String videopath;
	private Properties props;

	public Video() {
		super();
	}

	public Video(String picPath, String videopath, Properties props) {
		super();
		this.picPath = picPath;
		this.videopath = videopath;
		this.props = props;
	}

	public String getPicPath() {
		return VIDEO_DEFAULT_PREFIX_PATH + picPath;
	}

	public void setPicPath(String picPath) {
		this.picPath = picPath;
	}

	public String getVideopath() {
		return VIDEO_DEFAULT_PREFIX_PATH + videopath;
	}

	public void setVideopath(String videopath) {
		this.videopath = videopath;
	}

	public Properties getProps() {
		return props;
	}

	public void setProps(Properties props) {
		this.props = props;
	}
	
	public String getName(){
		return props.getProperty(Video.NAME);
	}
	
	public String getIntroduction(){
		return props.getProperty(Video.INTRODUCTION);
	}
	
	public String getStatus(){
		return props.getProperty(Video.STATUS);
	}
	
	public String getCategoryId(){
		return props.getProperty(Video.CATEGORY_ID);
	}
	
	public String getSimpleUploadDate(){
		String updateTime = props.getProperty(Video.UPLOAD_DATE);
		return updateTime.split(" ")[0];
	}
	
	public String getStatusInText(){
		String status = props.getProperty(Video.STATUS);
		if(status == null || status.length() == 0){
			return "已编辑";
		}else if(status.equals("1")){
			return "已编辑";
		}else if(status.equals("2")){
			return "已发布";
		}else{
			return "未编辑";
		}
	}
	
	
	public static final String CATEGORY_ID = "categoryId";
	public static final String CATEGORY_NAME = "categoryName";
	public static final String VIDEO_ID = "videoId";   // 全局唯一,这就是文件名
	public static final String NAME = "name";
	public static final String INTRODUCTION = "introduction";
	public static final String LENGTH = "length";
	public static final String UPLOAD_DATE = "uploadDate"; //2012-01-02 00:00:00
	public static final String COUNT = "count";
	public static final String STATUS = "status";	// 0:未编辑; 1:已编辑但是未发布; 2:已编辑并且已发布
	public static final String STATUS_UNEDITED = "0";
	public static final String STATUS_UNPUBLISHED = "1";
	public static final String STATUS_PUBLISHED = "2";
	
	public static final String VIDEO_DEFAULT_PREFIX_PATH = "/videos";
	
	public static final String DEFAULT_PIC = "default.jpg";

	@Override
	public int compareTo(Video video) {
		String thisUploadTime = props.getProperty(UPLOAD_DATE);
		String videoUploadTime = video.getProps().getProperty(UPLOAD_DATE);
		return thisUploadTime.compareTo(videoUploadTime);
	}

	@Override
	public boolean equals(Object anObject) {
		Video item = (Video)anObject;
		if(videopath == null || item.getVideopath() == null){
			return false;
		}
		return videopath.equals(item.getVideopath());
	}
	
	public void store(){
		String videoRootPath = System.getProperty("user.dir");
		File videoFolder = new File( videoRootPath + "/videos");
		if(!videoFolder.exists()){
			videoFolder = new File( videoRootPath + "/src/main/webapp/videos");
			if(!videoFolder.exists()){
				String catalinaHome = System.getenv("VIDEO_HOME");
				String videoFolderPath = "";
				if(VideoMgr.WEBSITE_NAME == null || VideoMgr.WEBSITE_NAME.length() == 0){
					videoFolderPath = catalinaHome + "/webapps/videos";
				}else{
					videoFolderPath = catalinaHome + "/webapps/" + VideoMgr.WEBSITE_NAME + "/videos";
				}
				videoFolder = new File(videoFolderPath);
			}
		}
		
		FileOutputStream fos = null;
		String filePath = videoFolder + "/" + videopath.substring(0, videopath.length() - 4) + ".properties";
		File propFile = new File(filePath);
		try {
			if(!propFile.exists()){
				propFile.createNewFile();
			}
			
			fos = new FileOutputStream(filePath);
			props.store(fos, "");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
